package com.project.controller;

import com.project.bean.UserBean;
import com.project.util.UploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;

@RestController
@RequestMapping("user")
public class UserController {

    @RequestMapping("add")
    public String add(UserBean user,@RequestParam("face") MultipartFile mf) throws IOException {
        String fileName = UploadUtil.upload(mf,"static/html/userImg");

        user.setImgPath(fileName);
        System.out.println(user);
        return "ok";
    }

    @RequestMapping("land")
    public String land(String userName, String pwd,
                       HttpServletRequest request){
        if("java".equalsIgnoreCase(userName) &&
                "123".equalsIgnoreCase(pwd)){
            request.getSession().setAttribute("loginUser","java");
            return "ok";
        }

        return "no";
    }

    @RequestMapping("test")
    public String test(HttpServletRequest request){
        String name = request.getParameter("name");


        return name;
    }
}
